/**
 *
 */
package com.dhcc.cdbp.orika;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

import java.util.Date;
import java.util.UUID;

import io.github.benas.jpopulator.api.Populator;
import io.github.benas.jpopulator.impl.PopulatorBuilder;
import ma.glasnost.orika.CustomMapper;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.converter.ConverterFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.dhcc.cdbpdc.entity.baseindex.PatientMainIndex;
import com.dhcc.cdbpdc.entity.emr.TestRec;
import com.dhcc.cdbpdc.entity.hbr.Patient;
import com.dhcc.cdbpdc.entity.healthindex.DiagDetailIndex;

/**
 * @author 谢进军
 *
 */
public class OrikaTest {
	private static MapperFactory mapperFactory = null;
	Populator populator = null;
	@Before
	public void setUp() throws Exception {
		 mapperFactory = new DefaultMapperFactory.Builder().build();
		 populator = new PopulatorBuilder().build();
	}

	@Test
	public void test() {
//		mapperFactory.classMap( Patient.class, PatientMainIndex.class );
		Patient patient = populator.populateBean( Patient.class );
		MapperFacade mapper = mapperFactory.getMapperFacade();
		PatientMainIndex mainIndex = mapper.map( patient, PatientMainIndex.class );
		System.out.println(ToStringBuilder.reflectionToString(patient, ToStringStyle.MULTI_LINE_STYLE));
		System.out.println(ToStringBuilder.reflectionToString(mainIndex, ToStringStyle.MULTI_LINE_STYLE));
		assertThat( mainIndex.getId(), is(equalTo(patient.getId())));
	}
	//测试
	@Test public void testTestRecToDiagDetailIndex(){
		TestRec testRec = populator.populateBean( TestRec.class);
		MapperFacade mapper = mapperFactory.getMapperFacade();
		DiagDetailIndex diagDetailIndex = mapper.map(testRec, DiagDetailIndex.class);
		assertEquals(diagDetailIndex.getId(), testRec.getId());//不映射
		assertEquals(diagDetailIndex.getDataStatus(),testRec.getDataStatus());//不映射
		assertEquals(diagDetailIndex.getCreateTime(),testRec.getCreateTime());//不映射
		assertEquals(diagDetailIndex.getUpdateTime(),testRec.getUpdateTime());//不映射
		assertEquals(diagDetailIndex.getBusinessNo(),testRec.getBusinessNo());//不映射
		assertEquals(diagDetailIndex.getDataRank(),testRec.getDataRank());//不映射
//		assertEquals(diagDetailIndex.getBusinessSign(),testRec.getBusinessSign());//不映射
		assertEquals(diagDetailIndex.getOrgCode(),testRec.getOrgCode());//不映射
		assertEquals(diagDetailIndex.getDiagTypeCode(),testRec.getDiagTypeCode());//不映射
		assertEquals(diagDetailIndex.getDiagNo(),testRec.getDiagNo());//不映射
//		assertEquals(diagDetailIndex.getHealthRecNo(),testRec.getHealthRecNo());//不映射
//		assertEquals(diagDetailIndex.getFieldId(),testRec.getFieldId());//不映射
//		assertEquals(diagDetailIndex.getTableEnName(),testRec.getTableEnName());//不映射
//		assertEquals(diagDetailIndex.getTableCnName(),testRec.getTableCnName());//不映射
//		assertEquals(diagDetailIndex.getDepartNo(),testRec.getDepartNo());//不映射
//		assertEquals(diagDetailIndex.getEventOrderNum(),testRec.getEventOrderNum());//不映射
	}
	/**
	 *
	 */
	@Test public void testTestRecToDiagDetailIndex2(){
		TestRec testRec = populator.populateBean( TestRec.class);
		ConverterFactory converterFactory = mapperFactory.getConverterFactory();
		converterFactory.registerConverter("myConverterIdValue", new UuidToIdConverter());
		//如果不是按照缺省的映射关系的话，就要注册一个classMap
		mapperFactory.classMap(TestRec.class, DiagDetailIndex.class)
			.customize(
				new CustomMapper<TestRec,DiagDetailIndex>() {
					 public void mapAtoB(TestRec testRec, DiagDetailIndex diagDetailIndex, MappingContext context) {
						 diagDetailIndex.setId( UUID.randomUUID().toString().replace("-", "") );
						 diagDetailIndex.setDataStatus( '1' );
						 diagDetailIndex.setCreateTime( new Date() );
						 diagDetailIndex.setTableCnName("检验记录"+ testRec.getTestProjCategoryName() );
						 diagDetailIndex.setTableEnName( "t_test_rec" );
						 diagDetailIndex.setDiagDate( testRec.getBusinessTime() == null ? new Date() : testRec.getBusinessTime() );
						 diagDetailIndex.setEventOrderNum( "39" );
					 }
				}
		    )
			.fieldAToB( "businessUpdateTime", "businessUpdateTime" )
			.fieldAToB( "businessNo", "businessNo" )
			.fieldAToB( "dataRank", "dataRank" )
			.fieldAToB( "orgCode", "orgCode" )
			.fieldAToB( "diagTypeCode", "diagTypeCode" )
			.fieldAToB( "diagNo", "diagNo" )
			.fieldAToB( "id", "fieldId" )
			.fieldAToB( "testApplyDepartNo", "departNo" )
		  .byDefault()
	    .register();
		MapperFacade mapper = mapperFactory.getMapperFacade();
		DiagDetailIndex diagDetailIndex = mapper.map(testRec, DiagDetailIndex.class);
		System.out.println( ToStringBuilder.reflectionToString( testRec, ToStringStyle.MULTI_LINE_STYLE));
		System.out.println( ToStringBuilder.reflectionToString( diagDetailIndex, ToStringStyle.MULTI_LINE_STYLE));
		assertNotEquals(diagDetailIndex.getId(), testRec.getId());//不映射
		assertEquals(String.valueOf(diagDetailIndex.getDataStatus()),"1");//不映射
		assertNotEquals(diagDetailIndex.getCreateTime(),testRec.getCreateTime());//不映射
		assertEquals(diagDetailIndex.getUpdateTime(),testRec.getUpdateTime());//不映射
		assertEquals(diagDetailIndex.getBusinessNo(),testRec.getBusinessNo());//不映射
		assertEquals(diagDetailIndex.getDataRank(),testRec.getDataRank());//不映射
		assertEquals(diagDetailIndex.getBusinessSign(),null);//不映射
		assertEquals(diagDetailIndex.getOrgCode(),testRec.getOrgCode());//不映射
		assertEquals(diagDetailIndex.getDiagTypeCode(),testRec.getDiagTypeCode());//不映射
		assertEquals(diagDetailIndex.getDiagNo(),testRec.getDiagNo());//不映射
		assertEquals(diagDetailIndex.getHealthRecNo(),null);//不映射
		assertEquals(diagDetailIndex.getFieldId(),testRec.getId());//不映射
		assertEquals(diagDetailIndex.getTableEnName(),"t_test_rec");//不映射
		assertEquals(diagDetailIndex.getTableCnName(),"检验记录"+ testRec.getTestProjCategoryName() );
		assertEquals(diagDetailIndex.getDepartNo(),testRec.getTestApplyDepartNo());//不映射
		assertEquals(diagDetailIndex.getEventOrderNum(),"39");//不映射
	}

	@After
	public void tearDown() throws Exception {
	}
}
